package com.udf.hive.udf.idcard;

import java.util.Calendar;
import java.util.Date;
import java.util.logging.Logger;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

import jodd.util.StringUtil;

/**
 * 身份解析
 * 
 */
public class IdCardParseUDF extends UDF
{

    private static Logger logger = Logger.getLogger("IdCardParseUDF");

    /**
     * 数据处理逻辑
     * 
     * @param str
     * @param type
     *            0:查询生日 1:查询年龄 2:查询性别
     * @param sex
     *            性别类型 0:男女 1:男0女1 2:男1女0 3:男1女2 4:男2女1
     * @return
     */
    public Text evaluate(Text str, Text type, Text sex) {
        if (null == str || null == type) {
            return null;
        }

        String value = String.valueOf(str).trim();
        String queryType = String.valueOf(type).trim();
        String sexType = String.valueOf(sex).trim();
        if (value.length() < 18 || value.length() != 18) {
            return null;
        }
        String result = null;

        String birthday = "";

        if ("0".equals(queryType)) {
            // 查询生日
            birthday = value.substring(6, 14);
            result = birthday;
        }
        else if ("1".equals(queryType)) {
            // 查询年龄
            int age = getAge(value);
            result = String.valueOf(age);
        }
        else if ("2".equals(queryType)) {
            // 查询性别
            result = getSex(value, sexType);
        }

        return new Text(result);
    }

    // 获取性别
    private String getSex(String value, String sexType) {
        // 获取最后第二位
        String sexNo = value.substring(value.length() - 2, value.length() - 1);

        String sexValue = "";
        // 返回性别值 0:男女 1:男0女1 2:男1女0 3:男1女2 4:男2女1
        if (Integer.parseInt(sexNo) % 2 == 0) {
            // 女
            if (StringUtil.isNotBlank(sexType)) {
                switch (sexType) {
                    case "0":
                        sexValue = "女";
                        break;
                    case "1":
                        sexValue = "1";
                        break;
                    case "2":
                        sexValue = "0";
                        break;
                    case "3":
                        sexValue = "2";
                        break;
                    case "4":
                        sexValue = "1";
                        break;
                    default:
                        sexValue = "女";
                        break;
                }
            }
        }
        else {
            // 男
            if (StringUtil.isNotBlank(sexType)) {
                switch (sexType) {
                    case "0":
                        sexValue = "男";
                        break;
                    case "1":
                        sexValue = "0";
                        break;
                    case "2":
                        sexValue = "1";
                        break;
                    case "3":
                        sexValue = "1";
                        break;
                    case "4":
                        sexValue = "2";
                        break;
                    default:
                        sexValue = "男";
                        break;
                }

            }
        }
        return sexValue;
    }

    // 计算年龄
    private int getAge(String value) {
        String birthday;
        // 获取身份证时间年月日
        birthday = value.substring(6, 14);
        String year = birthday.substring(0, 4);
        String month = birthday.substring(4, 6);
        String day = birthday.substring(6, 8);

        // 获取当前时间年月日
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int nowYear = calendar.get(1);
        int nowMonth = calendar.get(2) + 1;
        int nowDay = calendar.get(5);

        // 已过生日
        int age = 0;
        if (Integer.valueOf(month) < nowMonth
                || (Integer.valueOf(month) == nowMonth && Integer.valueOf(day) <= nowDay)) {
            age = nowYear - Integer.valueOf(year) + 1;
        }
        else {
            age = nowYear - Integer.valueOf(year);
        }
        return age;
    }

    public static void main(String[] args) {
        logger.info(String
                .valueOf(new IdCardParseUDF().evaluate(new Text("320582201801020119"), new Text("0"), new Text(""))));
    }
}
